Permalink
Browse files

Add Fanfiction.net generator

  • Loading branch information...
palfrey committed Dec 25, 2012
1 parent 471b652 commit babaf409b9dc8129f2c551744cd9afca0a51a72c
Showing with 36 additions and 1 deletion.
  1. +7 −1 README.md
  2. +29 −0 fanfiction.py
View
@@ -7,7 +7,7 @@ Run `make` to create the protocol buffers files
Usage
-----
-books.py \[options\] (<series name list>)*
+python books.py \[options\] (<series name list>)*
Options:
@@ -18,4 +18,10 @@ The list of series are in series.txt, and primarily consist of a series of
Regex's defining the title, content and next page link written in [Protocol
Buffers text format][pb]
+Generator
+---------
+Run "python fanfiction.py <a story URL from FanFiction.net>" to generate a new
+series entry for an arbitrary FanFiction.net story, which can them be copied
+into series.txt
+
[pb]: https://developers.google.com/protocol-buffers/docs/overview
View
@@ -0,0 +1,29 @@
+from sys import argv
+from urlgrab import Cache
+from codecs import open
+import re
+
+cache = Cache()
+url = argv[1]
+data = cache.get(url).read()
+open("dump", "wb", "utf-8").write(data)
+
+title = re.search("<title>(.+?) Chapter \d+: .+?(, a .+? fanfic)", data)
+title = title.groups()
+author = re.search("Author: <a href='/u/\d+/[^']+'>([^<]+)</a>", data)
+author = author.groups()[0]
+name = re.search("self.location = '/s/\d+/'\+ this\.options\[this.selectedIndex\]\.value \+ '/(.+?)';", data)
+name = name.groups()[0]
+id = re.search("/s/(\d+)", url)
+id = id.groups()[0]
+
+print """series {
+ name: "%s"
+ description: "%s"
+ author: "%s"
+ titlePattern: "%s (.+?), a"
+ startPage: "http://www.fanfiction.net/s/%s/1/%s"
+ contentPattern: "<div class='storytext xcontrast_txt' id='storytext'>(.+?)</div>(.*?)</div><div style='height:5px'>"
+ nextPattern: "Value='&nbsp;Next &gt;&nbsp;' onClick=\"self.location='([^']+)"
+}"""%(title[0].replace(" ",""), title[0], author, title[0], id, name)
+

0 comments on commit babaf40

Please sign in to comment.